﻿using StudentMgmt.Model;
using StudentMgmt.Model.DataContext;
using StudentMgmt.SQLServerDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentMgmt.IDAL;
using System.Data;
using StudentMgmt.DBUtility;

namespace StudentMgmt.SQLServerDAL
{
    public class Punishment:IPunishment
    {
        public int QueryPunishment(string StuNo, out DataTable objTable)
        {
            int val = 0;
            objTable = new DataTable();
            StringBuilder builder = new StringBuilder();
            builder.Append("select P.Punishment_Id as ID,L.Punish_Descriptions as '处罚等级', P.Punishment_Descriptions as '处罚详情',P.Create_Date as '创建时间',case when P.Punishment_Enable='T' then '通过' else '未通过' end '审批' ");
            builder.Append("from Punishment as P ");
            builder.Append("inner join Punish_Levels_Config as L on P.Punishment_Code=L.Punish_Code ");
           builder.Append("where 1=1 ");
            if (StuNo.Length > 0)
            {
                builder.Append("and P.StuNo = '" + StuNo );
            }
            builder.Append("';");
            val = SqlHelper.RunSQLReturnDataTable(builder.ToString(), out objTable);
            return val;
        }

        public int GetMaxPunishmentId()
        {
            int val = 0;
            DataTable myTable = new DataTable();
            StringBuilder builder = new StringBuilder();
            builder.Append("select MAX(Punishment_Id) as MaxId ");
            builder.Append("from Punishment where 1=1");
            SqlHelper.RunSQLReturnDataTable(builder.ToString(), out myTable);
            val = int.Parse(myTable.Rows[0]["MaxId"].ToString());
            return val;
        }

        public IList<PunishmentInfo> GetPunishments()
        {
            StudentMgmtDataContext db = new StudentMgmtDataContext();
            var punishments = from s in db.Punishment
                              select s;

            return punishments.ToList();
        }

        public PunishmentInfo GetPunishment(string StuNo)
        {
            StudentMgmtDataContext db = new StudentMgmtDataContext();
            var punishment = db.Punishment.Single(s => s.StuNo == StuNo);

            return punishment;
        }

        public void InsertPunishement(PunishmentInfo Punishment)
        {
            StudentMgmtDataContext db = new StudentMgmtDataContext();

            db.Punishment.InsertOnSubmit(Punishment);
            db.SubmitChanges();
        }
    }
}
